﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;

namespace ERPStore.IOC
{
	public class ServiceBusDependencyResolver : Ariane.IDependencyResolver
	{
		public ServiceBusDependencyResolver(Microsoft.Practices.Unity.IUnityContainer container)
		{
			Container = container;
		}

		public Microsoft.Practices.Unity.IUnityContainer Container { get; set; }

		public IEnumerable<object> GetAllServices()
		{
			return Container.Registrations;
		}

		public object GetService(Type serviceType)
		{
			object service = null;
			try
			{
				service = Container.Resolve(serviceType);
			}
			catch
			{
				var logger = Container.Resolve<Logging.ILogger>();
				foreach (var item in Container.Registrations)
				{
					logger.Debug("name:{0} type:{1} mapTo:{2} lifeTime:{3}", item.Name, item.RegisteredType, item.MappedToType, item.LifetimeManagerType);
				}
				throw;
			}

			return service;
		}

		public IEnumerable<object> GetServices(Type serviceType)
		{
			return Container.ResolveAll(serviceType);
		}
	}
}
